package com.msb.c11;

import java.util.concurrent.TimeUnit;

/**
 * @author: xw
 * @create: 2022/10/3 14:06
 * @description:reentrantlock用于替代synchronized
 *  *本例中由于m1锁定this,只有m1执行完毕的时候,m2才能执行
 *  *这里是复习synchronized最原始的语义
 */
public class Code01_ReentrantLock1 {
    synchronized void m1(){
        for (int i = 0; i <10; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i == 5){
                m2();
            }
            System.out.println(i);
        }
    }
    synchronized void m2(){
        System.out.println("m2-----");
    }

    public static void main(String[] args) {
        Code01_ReentrantLock1 t = new Code01_ReentrantLock1();
        new Thread(t::m1).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        new Thread(t::m2).start();
    }
}
